package com.jdrx.ocp.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jdrx.ocp.beans.constans.EDeleteFlag;
import com.jdrx.ocp.beans.constans.ELoginDevice;
import com.jdrx.ocp.beans.dto.userStats.DateDTO;
import com.jdrx.ocp.beans.entity.UserLoginStatsPO;
import com.jdrx.ocp.dao.UserLoginStatsDAO;
import com.jdrx.ocp.utils.DateUtil;
import com.jdrx.platform.jdbc.beans.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@EnableScheduling
public class UserLoginStatsService {
    @Autowired
    private UserLoginStatsDAO dao;
    //是否是今天第一个登陆的人
    private static boolean isFrist = true;

    public PageVO<UserLoginStatsPO> loginStats(DateDTO dto) {
        Page<UserLoginStatsPO> page = PageHelper.startPage(dto.getPageNum(), dto.getPageSize(), "create_at DESC");
        dao.listBy(BeanMap.create(dto));
        return new PageVO<>(page);
    }

    public void updateLoginRecord(Integer deviceType) {

        UserLoginStatsPO lastRecord = dao.findLastRecord();

        //今天第一次登陆的人将检查今天自动任务创建的记录是否存在,不存在则再次创建
        if (isFrist) {
            //先查看这条记录是否是今天,如果不是就创建一条记录
            if (lastRecord == null || !DateUtil.isToday(lastRecord.getCreateAt())) {
                create(lastRecord,deviceType);
                isFrist = false;
                return;
            }
        }

        lastRecord.setCurrentYearLogin(lastRecord.getCurrentYearLogin() + 1);
        lastRecord.setCurrentMonthLogin(lastRecord.getCurrentMonthLogin() + 1);
        lastRecord.setTotalLogin(lastRecord.getTotalLogin() + 1);
        if (ELoginDevice.PC.getKey().equals(deviceType)) {
            lastRecord.setPcLogin(lastRecord.getPcLogin() + 1);
        } else {
            lastRecord.setAppLogin(lastRecord.getAppLogin() + 1);
        }
        dao.update(lastRecord);
    }

    //当今天数据不存在时,创建
    public void create(UserLoginStatsPO lastRecord,Integer deviceType) {
        UserLoginStatsPO po = new UserLoginStatsPO();
        Date now = new Date();

        po.setTotalLogin(1L);
        po.setCreateAt(now);
        po.setDelFlag(Integer.valueOf(EDeleteFlag.NORMAL.getKey()));

        if (ELoginDevice.PC.getKey().equals(deviceType)) {
            po.setPcLogin(1L);
            po.setAppLogin(0L);
        } else {
            po.setAppLogin(1L);
            po.setPcLogin(0L);
        }

        if (lastRecord != null) {
            //是否是本月
            if (DateUtil.isThisMonth(lastRecord.getCreateAt())) {
                po.setCurrentMonthLogin(lastRecord.getCurrentMonthLogin());
            } else {
                po.setCurrentMonthLogin(1L);
            }
            //是否是本年
            if (DateUtil.isThisYear(lastRecord.getCreateAt())) {
                po.setCurrentYearLogin(lastRecord.getCurrentYearLogin());
            } else {
                po.setCurrentYearLogin(1L);
            }
        } else {
            po.setCurrentMonthLogin(1L);
            po.setCurrentYearLogin(1L);
        }
        dao.insert(po);
    }

    //系统定时任务
    @Scheduled(cron = "0 0 0 * * ?")
    public void createBySystem() {
        //重置是否第一个登陆进来的人
        isFrist = true;
        UserLoginStatsPO lastRecord = dao.findLastRecord();

        lastRecord.setAppLogin(0L);
        lastRecord.setPcLogin(0L);
        lastRecord.setTotalLogin(0L);
        lastRecord.setDelFlag(Integer.valueOf(EDeleteFlag.NORMAL.getKey()));

        //是否是本月
        if (!DateUtil.isThisMonth(lastRecord.getCreateAt())) {
            lastRecord.setCurrentMonthLogin(0L);
        }
        //是否是本年
        if (!DateUtil.isThisYear(lastRecord.getCreateAt())) {
            lastRecord.setCurrentYearLogin(0L);
        }
        lastRecord.setCreateAt(new Date());

        dao.insert(lastRecord);
    }
}
